//
//  ShareManagerMob.m
//  SYRentCar
//
//  Created by linjw on 14/12/25.
//  Copyright (c) 2014年 DC. All rights reserved.
//

#import "ShareManagerMob.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "WXApi.h"
//#import "WeiboApi.h"
//#import "WeiboSDK.h"
#import <ShareSDKConnector/ShareSDKConnector.h>


@implementation ShareManagerMob

SingleM(ShareManagerMob)


+ (void)RegisterSharePlatformInfo{
    [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
        [platformsRegister setupWeChatWithAppId:kSHShareAppIdWeChat appSecret:kSHShareAppSecretWeChat];
        [platformsRegister setupQQWithAppId:kSHShareAppIdQQ appkey:kSHShareAppKeyQQ];
    }];
    
}

-(NSMutableDictionary *)shareContentWithTitle:(NSString *)title
                                      Content:(NSString *)contentStr
                                     ImageObj:(id)imageObj
                                       StrURL:(NSString *)strURL
                                    MediaType:(SSDKContentType)mediaType
                                    forPlatformSubType:(SSDKPlatformType)platformType
{
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if (platformType == SSDKPlatformSubTypeWechatTimeline ||
             platformType == SSDKPlatformSubTypeWechatSession) {
        NSData * imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageObj]];
        UIImage *  thumbImage = [UIImage imageWithData:imgData];
        thumbImage = [self scaleToSize:thumbImage size:CGSizeMake(190, 190)];
        if (imgData.length > 30000) {// 32K = 32*1024 微信限制32k缩略图 200*200 尺寸
            imgData = UIImageJPEGRepresentation(thumbImage, 0.01);
            thumbImage = [UIImage imageWithData:imgData];
        }
        [shareParams SSDKSetupWeChatParamsByText:contentStr?:@""
                                           title:title?:@"子壹财富"
                                             url:[NSURL URLWithString:strURL]
                                      thumbImage:thumbImage
                                           image:@[imageObj]
                                    musicFileURL:nil
                                         extInfo:nil
                                        fileData:nil
                                    emoticonData:nil
                             sourceFileExtension:nil
                                  sourceFileData:nil
                                            type:mediaType
                              forPlatformSubType:platformType];
       
//
//        [shareParams SSDKSetupWeChatParamsByText:contentStr?:@""
//                                           title:title?:@"子壹财富"
//                                             url:[NSURL URLWithString:strURL]
//                                      thumbImage:thumbImage
//                                           image:@[imageObj]
//                                    musicFileURL:nil
//                                         extInfo:nil
//                                        fileData:nil
//                                    emoticonData:nil
//                                            type:mediaType
//                              forPlatformSubType:platformType];
        
    }else{
        [shareParams SSDKSetupShareParamsByText:contentStr?:@""
                                         images:@[imageObj]
                                            url:[NSURL URLWithString:strURL]
                                          title:title?:@"子壹财富"
                                           type:mediaType];
    }

//    [shareParams SSDKSetupWeChatParamsByText:contentStr?:@"" title:title?:@"久惠商城" url:[NSURL URLWithString:strURL] thumbImage:nil image:<#(id)#> musicFileURL:<#(NSURL *)#> extInfo:<#(NSString *)#> fileData:<#(id)#> emoticonData:<#(id)#> sourceFileExtension:<#(NSString *)#> sourceFileData:<#(id)#> type:<#(SSDKContentType)#> forPlatformSubType:<#(SSDKPlatformType)#>];

    return shareParams;

}


//分享单个平台信息
-(void)shareWithType:(SSDKPlatformType)shareType
          parameters:(NSMutableDictionary *)publishContent
              result:(SSDKShareStateChangedHandler)stateChangedHandler
{
    [ShareSDK share:shareType
         parameters:publishContent
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error)
     {
         [[UIApplication sharedApplication].keyWindow endEditing:YES];
         if (state == SSDKResponseStateSuccess)
         {
             NSLog(@"分享成功");
         }
         else if (state == SSDKResponseStateFail)
         {
             NSLog(@"分享失败,错误码:%ld错误描述:%@", (long)error.code, error.userInfo[@"error_message"]);
         }
         stateChangedHandler(state,userData,contentEntity,error);
     }];
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}




@end




